package com.allwees.bs.module.scm.bo;

import com.allwees.bs.module.oms.constant.EShippingType;
import com.allwees.bs.module.scm.constant.ELogisticsType;
import com.allwees.bs.module.scm.entity.ScmPickOrderSku;
import com.allwees.bs.module.scm.entity.ScmShippingOrder;
import com.allwees.core.common.constant.EStatus;
import com.allwees.core.common.util.MoneyUtil;
import lombok.Data;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Accessors(chain = true)
@Data
public class ScmOrderShippingBo {

    /** 发货类型 **/
    private EShippingType shippingType;

    /** 物流方式 **/
    private ELogisticsType logisticsType;

    /** 收货地址uuid **/
    private String receiveAddressUuid;

    /** 仓库 uuid **/
    private String wareHouseUuid;

    /** 发货时间 **/
    private Date shippingAt;

    /** 运单号 **/
    private String expressCode;

    //=========平台合作=======

    /** 预约年月日 **/
    private Date reserveDate;

    /** 预约时间范围 **/
    private String reserveRange;

    //=========三方物流=======

    /** 物流公司 **/
    private String logisticsUuid;

    /** 发货人 **/
    private String shippingUserUuid;

    /** order item **/
    private List<ScmPickOrderSku> pickOrderSkus;

    public ScmShippingOrder shippingOrder() {
        ScmShippingOrder shippingOrder = new ScmShippingOrder()
                .setExpressCode(expressCode)
                .setLogisticsUuid(logisticsUuid)
                .setLogisticsType(logisticsType)
                .setReserveDate(reserveDate)
                .setReserveRange(reserveRange)
                .setShippingAt(shippingAt)
                .setWarehouseUuid(wareHouseUuid)
                .setReceiveAddressUuid(receiveAddressUuid)
                .setShippingUserUuid(shippingUserUuid)
                .setLogisticsPackStatus(EStatus.SCM_WAIT_ENTER);

        ScmPickOrderSku pickOrderSku = pickOrderSkus.get(0);

        shippingOrder.setRetailerUuid(pickOrderSku.getRetailerUuid());
        shippingOrder.setSupplierUuid(pickOrderSku.getSupplierUuid());
        shippingOrder.setStoreUuid(pickOrderSku.getStoreUuid());

        shippingOrder.setShippingQuantity(pickOrderSkus.stream().mapToInt(ScmPickOrderSku::getShippingQuantity).sum());
        shippingOrder.setQuantity(pickOrderSkus.stream().mapToInt(ScmPickOrderSku::getQuantity).sum());
        shippingOrder.setAmt(pickOrderSkus.stream().map(o -> MoneyUtil.mul(o.getSupplyPrice(), o.getShippingQuantity())).reduce(BigDecimal.ZERO, BigDecimal::add));

        return shippingOrder;
    }
}
